/*
cAndy Apple Core: An Apple //e emulator engine
    - based almost entirely on AppleWin

Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2008, Tom Charlesworth, Michael Pohoreski, Nick Westgate
Copyright (C) 2009-2010, Garnet Ulrich

cAndy Apple Core is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

cAndy Apple Core is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with cAndy Apple Core; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "StdAfx.h"

#define  BUTTONTIME       5000

#define  DEVICE_NONE      0
#define  DEVICE_JOYSTICK  1
#define  DEVICE_KEYBOARD  2
#define  DEVICE_MOUSE     3

#define  MODE_NONE        0
#define  MODE_STANDARD    1
#define  MODE_CENTERING   2
#define  MODE_SMOOTH      3

typedef struct
{
    int device;
    int mode;
} joyinforec;

static const joyinforec joyinfo[5] = {{DEVICE_NONE,MODE_NONE},
                           {DEVICE_JOYSTICK,MODE_STANDARD},
                           {DEVICE_KEYBOARD,MODE_STANDARD},
                           {DEVICE_KEYBOARD,MODE_CENTERING},
                           {DEVICE_MOUSE,MODE_STANDARD}};

// Key pad [1..9]; Key pad 0,Key pad '.'; Left ALT,Right ALT
enum JOYKEY {	JK_DOWNLEFT=0,
				JK_DOWN,
				JK_DOWNRIGHT,
				JK_LEFT,
				JK_CENTRE,
				JK_RIGHT,
				JK_UPLEFT,
				JK_UP,
				JK_UPRIGHT,
				JK_BUTTON0,
				JK_BUTTON1,
				JK_OPENAPPLE,
				JK_CLOSEDAPPLE,
				JK_MAX
			};

const UINT PDL_MIN = 0;
const UINT PDL_CENTRAL = 127;
const UINT PDL_MAX = 255;

static BOOL  keydown[JK_MAX] = {FALSE};

static DWORD buttonlatch[3] = {0,0,0};
static BOOL  joybutton[3]   = {0,0,0};

DWORD joytype[2]            = {DEVICE_JOYSTICK,DEVICE_NONE};	// Emulation Type for joysticks #0 & #1

static BOOL  setbutton[3]   = {0,0,0};	// Used when a mouse button is pressed/released

static int   xpos[2]        = {PDL_CENTRAL,PDL_CENTRAL};  //gu - the location of the paddle (or joystick x axis)
static int   ypos[2]        = {PDL_CENTRAL,PDL_CENTRAL};  //gu - the location of the paddle (or joystick y axis)

static unsigned __int64 g_nJoyCntrResetCycle = 0;	// Abs cycle that joystick counters were reset

//===========================================================================
void CASetJoystick0(int x, int y, int b0, int b1) //gu - added this to feed values from java
{
    xpos[0] = x;
    ypos[0] = y;

    setbutton[0] = b0;
    setbutton[1] = b1;

}

//
// ----- ALL GLOBALLY ACCESSIBLE FUNCTIONS ARE BELOW THIS LINE -----
//

//===========================================================================
void JoyInitialize() { //gu - setting up for hardware joystick control - don't need - comes from java
    joyinfo[joytype[0]].device == DEVICE_MOUSE;  //gu - just to have some device type - DEVICE_NONE might disable joystick????
}


//===========================================================================

BYTE  JoyReadButton(WORD, WORD address, BYTE, BYTE, ULONG nCyclesLeft)
{
    address &= 0xFF;

    BOOL pressed = 0;
    switch (address) {

    case 0x61:
        pressed = (buttonlatch[0] || joybutton[0] || setbutton[0] || keydown[JK_OPENAPPLE]);
        buttonlatch[0] = 0;
        break;

    case 0x62:
        pressed = (buttonlatch[1] || joybutton[1] || setbutton[1] || keydown[JK_CLOSEDAPPLE]);
        buttonlatch[1] = 0;
        break;

    case 0x63:
        pressed = (buttonlatch[2] || joybutton[2] || setbutton[2]);
        buttonlatch[2] = 0;
        break;

    }
    return MemReadFloatingBus(pressed, nCyclesLeft);
}

//===========================================================================


static const double PDL_CNTR_INTERVAL = 2816.0 / 255.0;	// 11.04 (From KEGS)

BYTE  JoyReadPosition(WORD programcounter, WORD address, BYTE, BYTE, ULONG nCyclesLeft)
{
	int nJoyNum = (address & 2) ? 1 : 0;	// $C064..$C067

	CpuCalcCycles(nCyclesLeft);

	ULONG nPdlPos = (address & 1) ? ypos[nJoyNum] : xpos[nJoyNum];

	// This is from KEGS. It helps games like Championship Lode Runner & Boulderdash
	if(nPdlPos >= 255)
		nPdlPos = 280;

	BOOL nPdlCntrActive = g_nCumulativeCycles <= (g_nJoyCntrResetCycle + (unsigned __int64) ((double)nPdlPos * PDL_CNTR_INTERVAL));

	return MemReadFloatingBus(nPdlCntrActive, nCyclesLeft);
}

//===========================================================================
void JoyReset()
{
  int loop = 0;
  while (loop < JK_MAX)
    keydown[loop++] = FALSE;
}

//===========================================================================
BYTE  JoyResetPosition(WORD, WORD, BYTE, BYTE, ULONG nCyclesLeft)
{
	CpuCalcCycles(nCyclesLeft);
	g_nJoyCntrResetCycle = g_nCumulativeCycles;

	return MemReadFloatingBus(nCyclesLeft);
}


//===========================================================================
void JoyUpdatePosition()
{
	if (buttonlatch[0]) --buttonlatch[0];
	if (buttonlatch[1]) --buttonlatch[1];
	if (buttonlatch[2]) --buttonlatch[2];
}

//===========================================================================

DWORD JoyGetSnapshot(SS_IO_Joystick* pSS)
{
    pSS->g_nJoyCntrResetCycle = g_nJoyCntrResetCycle;
    return 0;
}

DWORD JoySetSnapshot(SS_IO_Joystick* pSS)
{
    g_nJoyCntrResetCycle = pSS->g_nJoyCntrResetCycle;
    return 0;
}

